/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.math.data.storage;

/**
 *
 * @author stewari1
 */
public class DoubleStorage extends Storage {

    /** Field description */
    private double val = 0;

    /**
     * Constructs ...
     *
     * @param val description
     */
    public DoubleStorage(double val) {

        super(StorageType.DOUBLE);

        this.val = val;
    }

    /**
     * Constructs ...
     */
    public DoubleStorage() {
        this(0);
    }

    double getVal() {
        return val;
    }

    /**
     * @throws ClassCastException
     */
    @Override
    public Storage plus(Storage storageType) {

        DoubleStorage doubleStorage = (DoubleStorage) storageType;

        return new DoubleStorage(this.val + doubleStorage.getVal());
    }

    @Override
    public Storage minus(Storage storageType) {

        DoubleStorage doubleStorage = (DoubleStorage) storageType;

        return new DoubleStorage(this.val - doubleStorage.getVal());
    }

    @Override
    public Storage times(Storage storageType) {

        DoubleStorage doubleStorage = (DoubleStorage) storageType;

        return new DoubleStorage(this.val * doubleStorage.getVal());
    }

    @Override
    public Storage inverse() {
        return new DoubleStorage(1.0 / this.getVal());
    }

    @Override
    public Storage divide(Storage storageType) {

        DoubleStorage doubleStorage = (DoubleStorage) storageType;

        return new DoubleStorage(this.val / doubleStorage.getVal());
    }

    @Override
    public Storage doubleStorage() {
        return this;
    }

    @Override
    public Storage floatStorage() {
        return new FloatStorage((float) this.getVal());
    }

    @Override
    public Storage intStorage() {
        return new IntegerStorage((int) this.getVal());
    }

    @Override
    public Storage longStorage() {
        return new LongStorage((long) this.getVal());
    }

    @Override
    public Storage shortStorage() {
        return new ShortStorage((short) this.getVal());
    }

    @Override
    public Storage copy() {
        return new DoubleStorage(this.val);
    }

    @Override
    public short shortValue() {
        return (short) this.val;
    }

    @Override
    public double doubleValue() {
        return this.val;
    }

    @Override
    public long longValue() {
        return (long) this.val;
    }

    @Override
    public float floatValue() {
        return (float) this.val;
    }

    @Override
    public boolean isLessThan(double epsilon) {
        return this.val < epsilon;
    }

    @Override
    public boolean isGreaterThan(double epsilon) {
        return this.val > epsilon;
    }

    @Override
    public boolean isEqualTo(double epsilon) {
        return this.val == epsilon;
    }

    @Override
    public void setValue(double val) {
        this.val = val;
    }

    @Override
    public Storage abs() {
        return new DoubleStorage(Math.abs(this.val));
    }
}
